package com.turman.demo

import android.content.Context
import android.content.res.Resources
import android.graphics.BitmapFactory
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.annotation.IdRes
import androidx.annotation.StringRes
import androidx.fragment.app.FragmentManager
import com.turman.blibrary.util.BDisplayUtil
import com.turman.builibrary.tab.bottom.BTabBottomInfo
import com.turman.builibrary.tab.bottom.BTabBottomLayout
import com.turman.common.tab.FragmentTabView
import com.turman.common.tab.TabViewAdapter
import com.turman.demo.fragment.CategoryFragment
import com.turman.demo.fragment.ChatFragment
import com.turman.demo.fragment.HomePageFragment
import com.turman.demo.fragment.ProfileFragment
import com.turman.demo.fragment.ShowFragment

/**
 * mainactivity logic object
 * @author buobao
 * @since 2024/3/12 11:04
 */
class MainActivityLogic {
    companion object {
        val CURRENT_PAGE_INDEX = "current_page_index"
    }

    private var fragmentTabView: FragmentTabView? = null
    private var tabBottomLayout: BTabBottomLayout? = null
    private var infoList: List<BTabBottomInfo<String>>? = null
    private var activityProvider: ActivityProvider
    private var currentIndex: Int = 0

    constructor(activityLogic: ActivityProvider, savedInstanceState: Bundle?) {
        this.activityProvider = activityLogic
        //已经初始化就跳过
        val savedIndex = savedInstanceState?.getInt(CURRENT_PAGE_INDEX)
        if (savedIndex != null) {
            currentIndex = savedIndex
        }
        initTabBottom()
    }

    private fun initTabBottom() {
        fragmentTabView = activityProvider.findViewById(R.id.fragment_tab_view)
        tabBottomLayout = activityProvider.findViewById(R.id.tab_bottom_layout)

        val bitmap =
            BitmapFactory.decodeResource(activityProvider.getResources(), R.drawable.fire, null)
        infoList =
            listOf(
                BTabBottomInfo(
                    "首页", "fonts/iconfont.ttf",
                    activityProvider.getString(R.string.if_home),
                    null,
                    "#ff656667",
                    "#ffd44949", HomePageFragment::class.java
                ),
                BTabBottomInfo(
                    "分类", "fonts/iconfont.ttf",
                    activityProvider.getString(R.string.if_category),
                    null,
                    "#ff656667",
                    "#ffd44949", CategoryFragment::class.java
                ),
                BTabBottomInfo("show", bitmap, bitmap,ShowFragment::class.java),
                BTabBottomInfo(
                    "信息", "fonts/iconfont.ttf",
                    activityProvider.getString(R.string.if_chat),
                    null,
                    "#ff656667",
                    "#ffd44949", ChatFragment::class.java
                ),
                BTabBottomInfo(
                    "我的", "fonts/iconfont.ttf",
                    activityProvider.getString(R.string.if_profile),
                    null,
                    "#ff656667",
                    "#ffd44949", ProfileFragment::class.java
                ),
            )

        fragmentTabView?.setAdapter(
            TabViewAdapter(
                activityProvider.getSupportFragmentManager(),
                infoList!!
            )
        )
        tabBottomLayout?.setBottomAlpha(0.85f)
        tabBottomLayout?.inflateInfo(infoList!!)
        tabBottomLayout?.defaultSelected(infoList?.get(currentIndex)!!)
        fragmentTabView?.setCurrentItem(currentIndex)
        tabBottomLayout?.addTabSelected { index, preInfo, nextInfo ->
            fragmentTabView?.setCurrentItem(infoList?.indexOf(nextInfo) ?: -1)
            currentIndex = index
            Toast.makeText(
                activityProvider.getBaseContext(),
                "click ${nextInfo.name}",
                Toast.LENGTH_SHORT
            ).show()
        }

        val fireTab = tabBottomLayout?.findTab(infoList?.get(2)!!)
        fireTab?.let {
            it.resetHeight(BDisplayUtil.dp2px(60f, activityProvider.getResources()))
        }
    }

    fun onSaveInstanceState(outState: Bundle) {
        //将当前显示的页面保存起来
        outState.putInt(CURRENT_PAGE_INDEX, currentIndex)
    }


    interface ActivityProvider {
        fun <T : View> findViewById(@IdRes id: Int): T
        fun getResources(): Resources
        fun getSupportFragmentManager(): FragmentManager
        fun getString(@StringRes resId: Int): String
        fun getBaseContext(): Context
    }
}